﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;

namespace EC.Admin.Controllers
{
    using EC.Application.Interface;

    using EC.Entity;
    using EC.Entity.Enum;
    using EC.Entity.Query;
    using EC.Entity.Request.Merchant;
    using EC.Entity.Request.Settle;
    using EC.Entity.Request.Token;
    using EC.Entity.Response.Merchant;
    using EC.Entity.Response.Token;
    using EC.Util;

    /// <summary>
    /// 商户管理
    /// </summary>
    public class MerchantController : BaseController
    {
        #region 注入
        /// <summary>
        /// 商户数据接口
        /// </summary>
        private readonly MerchantService _merchantService;

        /// <summary>
        /// 余额接口日志
        /// </summary>
        private readonly BalanceLogService _balanceLogService;

        /// <summary>
        /// 结算数据接口
        /// </summary>
        private readonly SettleService _settleService;

        /// <summary>
        /// 存款记录数据接口
        /// </summary>
        private readonly FrozenBalanceLogService _frozenBalanceLogService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataBase"></param>
        public MerchantController(LoginService loginService, MerchantService merchantService, BalanceLogService balanceLogService, SettleService settleService, FrozenBalanceLogService frozenBalanceLogService) : base(loginService)
        {
            _merchantService = merchantService;
            _balanceLogService = balanceLogService;
            _settleService = settleService;
            _frozenBalanceLogService = frozenBalanceLogService;
        }
        #endregion

        #region 系统

        #region 系统商户管理
        /// <summary>
        /// 商户
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.SyMerchantRecord0001)]
        public IActionResult SyMerchantRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.SyMerchantRecord0001)]
        public IActionResult SyMerchantRecordQuery(MerchantQuery query)
        {
            query.Type = (int)MerchantType.商户;

            var list = _merchantService.GetSyPager(query);
            return PartialView("_SyMerchantRecordQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.SyMerchantRecord0002)]
        public IActionResult SyMerchantsEdit(int? id)
        {
            var model = new MerchantResponse();
            if (id.HasValue)
            {
                model = _merchantService.Get(id.Value);
                if (model.Id > 0)
                {
                    model.PaymentRate = model.PaymentRate * 100;
                    model.DepositRate = model.DepositRate * 100;
                    model.DepositAliPayRate = model.DepositAliPayRate * 100;
                }
            }
            return View("SyMerchantsEdit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.SyMerchantRecord0002)]
        public JsonResult SaveSyMerchant(SaveMerchantRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //名称
                if (string.IsNullOrEmpty(request.Name))
                {
                    throw new Exception("商户名称不能为空.");
                }

                var nameReg = new Regex(@"^.{2,40}$");
                if (!nameReg.IsMatch(request.Name))
                {
                    throw new Exception("商户名称必须介于 2 和 40 之间");
                }
                //付款汇率
                if (string.IsNullOrEmpty(request.PaymentRate))
                {
                    throw new Exception("付款汇率最大保留两位小数，整数最多保留两位");
                }
                var paymentRateReg = new Regex(@"^(([1-9]\d{0,1})|0)(?:\.[0-9]{1,2})?$");
                if (!paymentRateReg.IsMatch(request.PaymentRate))
                {
                    throw new Exception("付款汇率最大保留两位小数，整数最多保留两位");
                }
                //存款汇率
                if (string.IsNullOrEmpty(request.DepositRate))
                {
                    throw new Exception("存款汇率最大保留两位小数，整数最多保留两位");
                }
                var depositRateReg = new Regex(@"^(([1-9]\d{0,1})|0)(?:\.[0-9]{1,2})?$");
                if (!depositRateReg.IsMatch(request.DepositRate))
                {
                    throw new Exception("存款汇率最大保留两位小数，整数最多保留两位");
                }
                //存款支付宝汇率
                if (string.IsNullOrEmpty(request.DepositAliPayRate))
                {
                    throw new Exception("存款支付宝汇率最大保留两位小数，整数最多保留两位");
                }
                var depositAliPayRateReg = new Regex(@"^(([1-9]\d{0,1})|0)(?:\.[0-9]{1,2})?$");
                if (!depositAliPayRateReg.IsMatch(request.DepositAliPayRate))
                {
                    throw new Exception("存款支付宝汇率最大保留两位小数，整数最多保留两位");
                }

                //单笔支付
                if (string.IsNullOrEmpty(request.PaymentSingle))
                {
                    throw new Exception("单笔支付不能为空");
                }
                var paymentSingleReg = new Regex(@"^(([1-9]\d{0,1})|0)?$");
                if (!paymentSingleReg.IsMatch(request.PaymentSingle))
                {
                    throw new Exception("单笔支付，整数最多保留两位");
                }
                //结算手续费
                if (string.IsNullOrEmpty(request.SettleSingle))
                {
                    throw new Exception("结算手续费不能为空");
                }
                var settleSingleReg = new Regex(@"^(([1-9]\d{0,1})|0)?$");
                if (!settleSingleReg.IsMatch(request.SettleSingle))
                {
                    throw new Exception("结算手续费，整数最多保留两位");
                }
                #endregion

                request.UserId = UserId;
                request.MerchantId = MerchantId;
                response = _merchantService.Save(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// Token
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.SyMerchantRecord0003)]
        public IActionResult Token(int merchantId)
        {
            var response = new TokenResponse();

            var model = _merchantService.GetToken(merchantId);
            if (model == null)
            {
                response.MerchantId = merchantId;
            }
            else
            {
                response = model;
            }
            return View(response);
        }

        /// <summary>
        /// Token新增、编辑
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.SyMerchantRecord0003)]
        public JsonResult SaveToken(SaveTokenRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //商户编号
                if (request.MerchantId <= 0)
                {
                    throw new Exception("参数错误.");
                }

                //AppId
                if (string.IsNullOrEmpty(request.AppId))
                {
                    throw new Exception("AppId不能为空.");
                }

                var appIdReg = new Regex(@"^[a-zA-Z0-9]{32}$");
                if (!appIdReg.IsMatch(request.AppId))
                {
                    throw new Exception("AppId由32位数字组成.");
                }

                //AppSecret
                if (string.IsNullOrEmpty(request.AppSecret))
                {
                    throw new Exception("AppSecret不能为空.");
                }

                var appSecretReg = new Regex(@"^[a-zA-Z0-9]{64}$");
                if (!appSecretReg.IsMatch(request.AppSecret))
                {
                    throw new Exception("AppSecret由64位字母或数字组成.");
                }

                response = _merchantService.SaveToken(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 卡商管理
        /// <summary>
        /// 卡商
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.CardMerchants0001)]
        public IActionResult CardMerchantRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.CardMerchants0001)]
        public IActionResult CardMerchantRecordQuery(MerchantQuery query)
        {
            query.Type = (int)MerchantType.卡商;

            var list = _merchantService.GetSyPager(query);
            return PartialView("_CardMerchantRecordQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.CardMerchants0001)]
        public IActionResult CardMerchantRecordEdit(int? id)
        {
            var model = new MerchantResponse();
            if (id.HasValue)
            {
                model = _merchantService.Get(id.Value);
            }
            return View("CardMerchantRecordEdit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.CardMerchants0001)]
        public JsonResult SaveCardMerchantRecord(SaveMerchantRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //名称
                if (string.IsNullOrEmpty(request.Name))
                {
                    throw new Exception("卡商团队名称不能为空.");
                }
                #endregion

                request.UserId = UserId;
                request.MerchantId = MerchantId;
                request.Type = (int)MerchantType.卡商;
                response = _merchantService.SaveFinance(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 收款记录
        /// </summary>
        /// <returns>视图</returns>
        public IActionResult Proxy(int? id)
        {
            var model = new MerchantResponse();
            if (id.HasValue)
            {
                model = _merchantService.Get(id.Value);
                if (model != null && !string.IsNullOrEmpty(model.ProxyId))
                {
                    var idsStr = model.ProxyId.Split(',');

                    var ids = new List<int>();

                    foreach (var item in idsStr)
                    {
                        ids.Add(Convert.ToInt32(item));
                    }

                    if (ids.Any())
                    {
                        model.yesMerchantResponse = _merchantService.GetMerchantByIds(ids);
                    }
                }
            }

            var list = _merchantService.GetList(20);

            if (list != null && list.Any())
            {
                model.noMerchantResponse = list;
            }

            return View("Proxy", model);
        }

        /// <summary>
        /// 设置代理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult SaveProxy(SaveProxyRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //名称
                //if (request.ProxyId.Count<=0)
                //{
                //    throw new Exception("选择商户.");
                //}
                #endregion

                request.UserId = UserId;
                request.MerchantId = request.MerchantId;
                response = _merchantService.SaveProxy(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 代理管理
        /// <summary>
        /// 代理
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentRecord0001)]
        public IActionResult AgentRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.AgentRecord0001)]
        public IActionResult AgentRecordQuery(MerchantQuery query)
        {
            query.Type = (int)MerchantType.代理;

            var list = _merchantService.GetSyPager(query);
            return PartialView("_AgentQuery", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentRecord0001)]
        public IActionResult AgentEdit(int? id)
        {
            var model = new MerchantResponse();
            if (id.HasValue)
            {
                model = _merchantService.Get(id.Value);
            }
            return View("AgentEdit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentRecord0001)]
        public JsonResult SaveAgent(SaveMerchantRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //名称
                if (string.IsNullOrEmpty(request.Name))
                {
                    throw new Exception("代理名称不能为空.");
                }
                #endregion

                request.UserId = UserId;
                request.MerchantId = MerchantId;
                request.Type = (int)MerchantType.代理;
                response = _merchantService.SaveFinance(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #endregion

        #region 代理

        #region 商户管理
        /// <summary>
        /// 查看
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0001)]
        public IActionResult AgentMerchantRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0001)]
        public IActionResult AgentMerchantRecordQuery(MerchantQuery query)
        {
            query.UserPermission = UserPermissions;
            query.MerchantIds = new List<int>() { -1 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }

            var list = _merchantService.GetPager(query);

            return PartialView("_AgentMerchantRecordQuery", list);
        }

        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0002)]
        public IActionResult Recharge(int id)
        {
            var model = _merchantService.Get(id);

            return View(model);
        }

        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0002)]
        public JsonResult SaveRecharge(BalanceRequest request)
        {
            request.UserId = UserId;
            var response = _merchantService.SaveRecharge(request);
            return Json(response);
        }

        /// <summary>
        /// 充值(带手续费)
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0003)]
        public IActionResult RechargeFee(int id)
        {
            var model = _merchantService.Get(id);

            return View(model);
        }

        /// <summary>
        /// 充值(带手续费)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0003)]
        public JsonResult SaveRechargeFee(BalanceRequest request)
        {
            request.UserId = UserId;
            var response = _merchantService.SaveRechargeFee(request);
            return Json(response);
        }

        /// <summary>
        /// 扣减余额
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0004)]
        public IActionResult LessBalance(int id)
        {
            var model = _merchantService.Get(id);
            return View(model);
        }

        /// <summary>
        /// 扣减余额
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0004)]
        public JsonResult SaveLessBalance(BalanceRequest request)
        {
            request.UserId = UserId;
            var response = _merchantService.SaveLessBalance(request);
            return Json(response);
        }

        /// <summary>
        /// 余额冻结
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0005)]
        public IActionResult FrozenBalance(int id)
        {
            var model = _merchantService.Get(id);
            return View(model);
        }

        /// <summary>
        /// 余额冻结
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.AgentMerchantRecord0005)]
        [HttpPost]
        public JsonResult SaveFrozenBalance(BalanceRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = request.MerchantId;
            var response = _merchantService.SaveFrozenBalance(request);
            return Json(response);
        }
        #endregion

        #region 结算记录
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.SettleRecord0001)]
        public IActionResult SettleRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.SettleRecord0001)]
        public JsonResult SettleRecordQuery(SettleQuery query)
        {
            query.MerchantIds = new List<int>() { -1 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }

            var list = _settleService.GetPager(query);

            return Json(list);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.SettleRecord0002)]
        [HttpPost]
        public JsonResult SettleAudit(AuditRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)SettleStatus.处理中;

            var response = _settleService.Audit(request);

            return Json(response);
        }

        /// <summary>
        /// 确认打款
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.SettleRecord0003)]
        [HttpPost]
        public JsonResult SettleConfirm(AuditRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)SettleStatus.已打款;

            var response = _settleService.Confirm(request);

            return Json(response);
        }

        /// <summary>
        /// 驳回
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.SettleRecord0004)]
        [HttpPost]
        public JsonResult SettleReject(AuditRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;
            request.Status = (int)SettleStatus.驳回;

            var response = _settleService.Reject(request);

            return Json(response);
        }

        /// <summary>
        /// 备注
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.SettleRecord0005)]
        public IActionResult SettleRemark(int id)
        {
            var model = _settleService.Get(id);

            return View(model);
        }

        /// <summary>
        /// 备注
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.SettleRecord0005)]
        [HttpPost]
        public JsonResult SaveSettleRemark(SettleRemarkRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = MerchantId;

            var response = _settleService.SaveRemark(request);

            return Json(response);
        }
        #endregion

        #region 账变记录
        /// <summary>
        /// 账变记录
        /// </summary>
        /// <returns>视图</returns>
        [AuthFilter(AuthCode.AccountChangeRecord0001)]
        public IActionResult AccountChangeRecord()
        {
            return View();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.AccountChangeRecord0001)]
        public IActionResult AccountChangeRecordQuery(BalanceLogQuery query)
        {
            query.MerchantIds = new List<int>();
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }
            query.UserId = UserId;
            var list = _balanceLogService.GetPager(query);

            return PartialView("_AccountChangeRecordQuery", list);
        }
        #endregion

        #region 冻结记录
        /// <summary>
        /// 查看
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.FrozenRecord0001)]
        public IActionResult FrozenRecord()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        [AuthFilter(AuthCode.FrozenRecord0001)]
        public IActionResult FrozenRecordQuery(FrozenBalanceLogQuery query)
        {
            query.UserId = UserId;
            query.MerchantIds = new List<int>() { -1 };

            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    query.MerchantIds.Add(Convert.ToInt32(item));
                }
            }

            var list = _frozenBalanceLogService.GetPager(query);

            return PartialView("_FrozenRecordQuery", list);
        }

        /// <summary>
        /// 解冻
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.FrozenRecord0002)]
        [HttpPost]
        public JsonResult Thaw(ThawRequest request)
        {
            request.UserId = UserId;
            request.MerchantId = request.MerchantId;
            var response = _merchantService.Thaw(request);
            return Json(response);
        }
        #endregion

        #region 充值记录
        /// <summary>
        /// 充值记录
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.RechargeRecord0001)]
        public IActionResult RechargeRecord()
        {
            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            ViewBag.YesterdayStartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.YesterdayEndTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 23:59:59");

            ViewBag.MonthStartTime = DateUtil.FirstDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 00:00:00");
            ViewBag.MonthEndTime = DateUtil.LastDayOfMonth(DateTime.Now).ToString("yyyy-MM-dd 23:59:59");

            var merchantIds = new List<int>() { 0 };
            //获取公司
            var merchant = _merchantService.Get(MerchantId);
            //验证
            if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
            {
                var ids = merchant.ProxyId.Split(',');
                foreach (var item in ids)
                {
                    merchantIds.Add(Convert.ToInt32(item));
                }
            }

            ViewBag.Merchants = _merchantService.GetMerchantByIds(merchantIds);

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>

        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 充值记录
        /// </summary>
        /// <returns>JResult</returns>
        [AuthFilter(AuthCode.RechargeRecord0001)]
        public JsonResult RechargeRecordQuery(RechargeRecordQuery query)
        {
            query.MerchantIds = new List<int>() { -1 };

            if (query.MerchantId.HasValue)
            {
                query.MerchantIds.Add(query.MerchantId.Value);
            }
            else
            {
                //获取公司
                var merchant = _merchantService.Get(MerchantId);
                //验证
                if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
                {
                    var ids = merchant.ProxyId.Split(',');
                    foreach (var item in ids)
                    {
                        query.MerchantIds.Add(Convert.ToInt32(item));
                    }
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _balanceLogService.GetRechargeRecordPager(query);

            return Json(list);
        }


        /// <summary>
        /// 导出记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.RechargeRecord0001)]
        public JsonResult RechargeRecordExport(RechargeRecordQuery query)
        {
            query.Page = 1;
            query.Rows = 500;
            query.MerchantIds = new List<int>() { -1 };

            if (query.MerchantId.HasValue)
            {
                query.MerchantIds.Add(query.MerchantId.Value);
            }
            else
            {
                //获取公司
                var merchant = _merchantService.Get(MerchantId);
                //验证
                if (merchant != null && !string.IsNullOrEmpty(merchant.ProxyId))
                {
                    var ids = merchant.ProxyId.Split(',');
                    foreach (var item in ids)
                    {
                        query.MerchantIds.Add(Convert.ToInt32(item));
                    }
                }
            }

            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            var list = _balanceLogService.GetRechargeRecordPager(query);

            list.StatusCode = $"{query.StartTime}-{query.EndTime}";
            return Json(list);
        }
        #endregion

        #endregion
    }
}
